package com.wsoft.hr.pm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wsoft.core.service.CommonMemberService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.*;
import com.wsoft.hr.pm.enums.HrProcessFlowStageEnum;
import com.wsoft.hr.pm.manager.HrPmMemberTemplateManager;
import com.wsoft.hr.pm.service.*;
import com.wsoft.hr.pm.vo.member.template.MemberTemplateVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Admin
 * @date 2025/3/7 15:55
 * @description:
 */
@Service
public class HrPmScoreServiceImpl implements IHrPmScoreService {
    @Resource
    private HrPmMemberTemplateManager memberTemplateManager;
    @Resource
    private CommonMemberService commonMemberService;
    @Resource
    private IHrPmMemberTemplateService memberTemplateService;
    @Resource
    private IHrPmMemberTemplateScoreService memberTemplateScoreService;
    @Resource
    private IHrPmMemberTemplateScopeScoreService memberTemplateScopeScoreService;

    @Resource
    private IHrPmMemberTemplateTargetScoreService memberTemplateTargetScoreService;

    @Resource
    private IHrPmMemberTemplateScopeService memberTemplateScopeService;

    @Resource
    private IHrPmMemberTemplateTargetService memberTemplateTargetService;


    @Override
    public void complete(String processDefKey, String processInstanceId, String businessKey) {
        //赋分
        HrPmMemberTemplateEntity memberTemplate = memberTemplateManager.getScoreByBusinessKey(businessKey);
        //人员考核表
        MemberTemplateVO detail = memberTemplateService.getMemberTemplateDetail(memberTemplate.getId());
        //赋值主表分数
        QueryWrapper<HrPmMemberTemplateScoreEntity> memberTemplateScoreQuery = new QueryWrapper<>();
        memberTemplateScoreQuery.lambda().eq(HrPmMemberTemplateScoreEntity::getMemberTemplateId, memberTemplate.getId())
                .orderByDesc(BaseEntity::getCreateTime);
        HrPmMemberTemplateScoreEntity templateScore = memberTemplateScoreService.list(memberTemplateScoreQuery).get(0);
        if(ObjectUtil.isNotEmpty(templateScore)){
            memberTemplate.setScore(templateScore.getScore());
            memberTemplate.setState(HrProcessFlowStageEnum.RESULT_CONFIRMATION.getType());
            memberTemplateManager.updateById(memberTemplate);
            //查询维度分数
            QueryWrapper<HrPmMemberTemplateScopeScoreEntity> memberTemplateScopeScoreQuery = new QueryWrapper<>();
            memberTemplateScopeScoreQuery.lambda().eq(HrPmMemberTemplateScopeScoreEntity::getMemberTemplateScoreId, templateScore.getId());
            List<HrPmMemberTemplateScopeScoreEntity> scopeScoreList = memberTemplateScopeScoreService.list(memberTemplateScopeScoreQuery);
            Map<Long, BigDecimal> scopeScoreMap = scopeScoreList.stream().collect(Collectors.toMap(HrPmMemberTemplateScopeScoreEntity::getScopeId, HrPmMemberTemplateScopeScoreEntity::getScore));
            //查询指标分数
            QueryWrapper<HrPmMemberTemplateTargetScoreEntity> memberTemplateTargetScoreQuery = new QueryWrapper<>();
            memberTemplateTargetScoreQuery.lambda().eq(HrPmMemberTemplateTargetScoreEntity::getMemberTemplateScoreId, templateScore.getId());
            List<HrPmMemberTemplateTargetScoreEntity> targetScoreList = memberTemplateTargetScoreService.list(memberTemplateTargetScoreQuery);
            Map<Long, BigDecimal> targetScoreMap = targetScoreList.stream().collect(Collectors.toMap(HrPmMemberTemplateTargetScoreEntity::getMemberTemplateTargetId, HrPmMemberTemplateTargetScoreEntity::getScore));
            detail.getScopeList().forEach(scope -> {
                if (ObjectUtil.isNotEmpty(scopeScoreMap)){
                    memberTemplateScopeService.update(Wrappers
                            .lambdaUpdate(HrPmMemberTemplateScopeEntity.class)
                            .set(HrPmMemberTemplateScopeEntity::getScore, scopeScoreMap.get(scope.getId()))
                            .eq(BaseEntity::getId, scope.getId()));
                }
                scope.getTemplateTargetHisList().forEach(target -> {
                    if (ObjectUtil.isNotEmpty(targetScoreMap)) {
                        memberTemplateTargetService.update(Wrappers
                                .lambdaUpdate(HrPmMemberTemplateTargetEntity.class)
                                .set(HrPmMemberTemplateTargetEntity::getScore, targetScoreMap.get(target.getMemberTemplateTargetId()))
                                .eq(BaseEntity::getId, target.getMemberTemplateTargetId()));
                    }
                });
            });
        }



        IHrPmScoreService.super.complete(processDefKey, processInstanceId, businessKey);
    }

    @Override
    public String getProcessDefKey() {
        return HrProcessFlowStageEnum.PERFORMANCE_SCORE.getProcessDefKey();
    }
}
